package com.doupi.partner.dal;

import com.doupi.partner.model.Profit;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;

import java.util.List;
import java.util.Map;

public interface ProfitMapper {
	int deleteByPrimaryKey(Long id);

	int insert(Profit record);

	Profit selectByPrimaryKey(Long id);

	int updateByPrimaryKeySelective(Profit record);

	int countProfit(Profit profit);

	List<Profit> queryProfits(Profit profit, RowBounds rowBounds);

	List<Profit> selectProfitForUpdate(Profit profit);

	Profit selectProfitForUpdateByPrimaryKey(Long id);

	@Select("<script>select count(1) from t_profit  <where> " + "<trim prefix=\"(\" suffix=\")\" prefixOverrides=\"OR\">"
			+ "<if test=\"administratorId != null\" > or administrator_id=#{administratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"businessAdministratorId != null\" > or business_administrator_id=#{businessAdministratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"superiorPartnerAdministratorId != null\" > or superior_partner_administrator_id=#{superiorPartnerAdministratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"cityAdministratorId != null\" > or city_administrator_id=#{cityAdministratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"substationAdministratorId != null\" > or substation_administrator_id=#{substationAdministratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"status != null\" > and status=#{status,jdbcType=TINYINT} </if>" + " </trim> and `type` = #{type,jdbcType=TINYINT} " + "</where></script>")
	int countProfitWithAnnotation(Profit profit);

	@Select("<script>select * from t_profit  <where> " + "<trim prefix=\"(\" suffix=\")\" prefixOverrides=\"OR\">"
			+ "<if test=\"administratorId != null\" > or administrator_id=#{administratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"businessAdministratorId != null\" > or business_administrator_id=#{businessAdministratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"superiorPartnerAdministratorId != null\" > or superior_partner_administrator_id=#{superiorPartnerAdministratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"cityAdministratorId != null\" > or city_administrator_id=#{cityAdministratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"substationAdministratorId != null\" > or substation_administrator_id=#{substationAdministratorId,jdbcType=BIGINT} </if>"
			+ "<if test=\"status != null\" > and status=#{status,jdbcType=TINYINT} </if>" + " </trim> and `type` = #{type,jdbcType=TINYINT} " + "</where> order by create_time desc</script>")
	@ResultMap("com.doupi.partner.dal.ProfitMapper.BaseResultMap")
	List<Profit> queryProfitWithAnnotation(Profit profit, RowBounds rowBounds);

	/**
	 * 首页汇总
	 * 
	 * @param administratorId
	 *            合伙人id
	 * @return 汇总数据
	 */
	@Select("call pro_top_statistics(#{administratorId})")
	Map<String, Object> topStatistics(Long administratorId);

	/**
	 * 小时累计收益
	 * 
	 * @param administratorId
	 *            合伙人ID
	 * @param beginDay
	 *            开始距离今天天数 0表示今天 1昨天...
	 * @param endDay
	 *            结束距离今天天数 0表示今天 1昨天...
	 * @return 收益图表数据
	 */
	@Select("call pro_accumulate_statistics_group_by_hour(#{administratorId}, #{beginDay}, #{endDay})")
	List<Map<String, Object>> accumulateStatisticsGroupByHour(@Param("administratorId") Long administratorId, @Param("beginDay") Integer beginDay, @Param("endDay") Integer endDay);

	/**
	 * 历史小时收益
	 * 
	 * @param administratorId
	 *            合伙人ID
	 * @param beginDay
	 *            开始距离今天天数 0表示今天 1昨天...
	 * @param endDay
	 *            结束距离今天天数 0表示今天 1昨天...
	 * @return 收益图表数据
	 */
	@Select("call pro_history_statistics_group_by_hour(#{administratorId}, #{beginDay}, #{endDay})")
	List<Map<String, Object>> historyStatisticsGroupByHour(@Param("administratorId") Long administratorId, @Param("beginDay") Integer beginDay, @Param("endDay") Integer endDay);

	/**
	 * 历史小时收益
	 *
	 * @param administratorId
	 *            合伙人ID
	 * @param beginDay
	 *            开始距离今天天数 0表示今天 1昨天...
	 * @param endDay
	 *            结束距离今天天数 0表示今天 1昨天...
	 * @return 收益图表数据
	 */
	@Select("call pro_history_statistics_group_by_date(#{administratorId}, #{beginDay}, #{endDay})")
	List<Map<String, Object>> historyStatisticsGroupByDate(@Param("administratorId") Long administratorId, @Param("beginDay") Integer beginDay, @Param("endDay") Integer endDay);

	/**
	 * 
	 * 昨日收益排行
	 **
	 * @param top
	 *            前几名
	 * 
	 * @return 排名收益数据
	 */
	@Select("call pro_yesterday_profit_rank(#{top})")
	List<Map<String, Object>> queryYesterdayProfitRank(Integer top);

}
